import pyglet
import tranlator
from music import my_muchal
from pyglet.gui import Slider
from pyglet.image import SolidColorImagePattern


class mainscene:
    def __init__(self, window):
        self.window = window
        self.batch = pyglet.graphics.Batch()

        self.trans = tranlator.my_translator()
        self.trans.compile_scripts()
        self.sorted_commands = self.trans.sorted_commands

        # 创建Slider图像 - 确保基础图像足够宽
        BASE = SolidColorImagePattern((100, 226, 253, 255)).create_image(300, 20)  # 宽度300，高度20
        KNOB = SolidColorImagePattern((255, 0, 0, 255)).create_image(20, 30)  # 宽度20，高度30

        # 创建Slider
        self.slider = Slider(
            x=100, y=500,  # 给Slider一个合适的位置
            base=BASE,
            knob=KNOB,
            batch=self.batch
        )

        # 设置事件处理 - 注意：on_change事件会传递两个参数
        self.slider.set_handler('on_change', self.on_slider_change)

        # 显示当前值的标签
        self.value_label = pyglet.text.Label(
            f"Value: {self.slider.value:.2f}",
            font_name='Arial',
            font_size=16,
            x=100, y=450,
            batch=self.batch
        )

        # 注册到窗口事件处理器
        window.push_handlers(self.slider)

    def on_slider_change(self, slider, value):
        """Slider值改变时的回调函数 - 注意接受两个参数"""
        print(f"Slider value: {value}")
        self.value_label.text = f"Value: {value:.2f}"

    def on_draw(self):
        self.window.clear()
        self.batch.draw()


class GameWindow(pyglet.window.Window):
    def __init__(self):
        super().__init__(caption="gcgal_editor", width=500, height=1000)
        self.main_scene = mainscene(self)

    def on_draw(self):
        self.main_scene.on_draw()


def main():
    window = GameWindow()
    pyglet.app.run()


if __name__ == "__main__":
    main()